package com.gxa.zhonglong.service.impl;

import com.gxa.zhonglong.common.ExceptionCode;
import com.gxa.zhonglong.exception.BusinessException;
import com.gxa.zhonglong.mapper.DiscountMapper;
import com.gxa.zhonglong.pojo.Discount;
import com.gxa.zhonglong.service.DiscountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @auther JJuventus
 * @date 2021/10/26 15:00
 */
@Service("DiscountService")
public class DiscountServiceImpl implements DiscountService {

    @Autowired
    private DiscountMapper discountMapper = null;


    @Override
    public List<Discount> select(String want, Integer page, Integer size) {
        Integer offside = (page-1)*size;
        List<Discount> discounts = discountMapper.select(want, offside, size);
        return discounts;
    }

    @Override
    public List<Discount> selectAll(String want) {
        return discountMapper.selectAll(want);
    }

    @Override
    public Integer add(Discount discount) throws BusinessException {
        Discount discount1 = discountMapper.selectByLimit(discount);
        if(discount1==null){
            Integer add = discountMapper.add(discount);
            return add;
        }else{
            throw new BusinessException(ExceptionCode.DUPLICATE_DISCOUNT);
        }
    }

    @Override
    public Integer delete(Long discountId) throws BusinessException {
        Discount discount = discountMapper.selectById(discountId);
        if(discount!=null){
            Integer delete = discountMapper.delete(discountId);
            return delete;
        }else{
            throw new BusinessException(ExceptionCode.NO_DISCOUNT);
        }
    }

    @Override
    public void update(Discount discount) throws BusinessException {
        Discount discount1 = discountMapper.selectById(discount.getDiscountId());
        if (discount1!=null){
            discountMapper.update(discount);
        }else{
            throw new BusinessException(ExceptionCode.NO_DISCOUNT);
        }
    }
}
